package com.yunc.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunc.common.utils.BaseConstant;
import com.yunc.modules.sys.entity.SysMenu;
import com.yunc.modules.sys.mapper.SysMenuMapper;
import com.yunc.modules.sys.service.ISysMenuService;
import com.yunc.modules.sys.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统菜单service层实现
 *
 * @author congcong tao
 * @date 2018/1/24 10:34
 */
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper,SysMenu> implements ISysMenuService {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenu> queryUserMenuList(Long userId) {
        List<String> menuIdList = sysUserService.queryMenuIdByUserId(userId);
        QueryWrapper<SysMenu> ew = new QueryWrapper<SysMenu>()
                .orderByAsc("order_num");
        List<SysMenu> list = super.list(ew);
        return this.getMenuTree(list, menuIdList, 0L);
    }

    @Override
    public List<SysMenu> queryUserMenuList() {
        return this.queryUserMenuList(null);
    }

    @Override
    public List<SysMenu> queryListParentId(Long parentId, List<String> menuIdList) {
        QueryWrapper<SysMenu> ew = new QueryWrapper<SysMenu>()
                        .eq("parent_id", parentId)
                        .orderByAsc("order_num");
        List<SysMenu> sysMenuList = super.list(ew);
        List<SysMenu> userMenuList = new ArrayList<>();
        for (SysMenu menu : sysMenuList) {
            Long id = menu.getId();
            if (menuIdList.contains(String.valueOf(id)) && !StringUtils.equals(menu.getType(), BaseConstant.MenuType.BUTTON.getValue())) {
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    @Override
    public List<Map<String, Object>> roleMenuTreeData(SysMenu sysMenu, Long roleId) {
        List<Map<String, Object>> trees;
        QueryWrapper<SysMenu> ew = new QueryWrapper<>(sysMenu);
        List<SysMenu> sysMenuList = this.list(ew);
        if (roleId != null) {
            // 获取menuId
            List<String> menuIdList = sysUserService.queryMenuIdByRoleId(roleId);
            trees = getTrees(sysMenuList, true, menuIdList, true);
        } else {
            trees = getTrees(sysMenuList, false, null, true);
        }
        return trees;
    }

    public List<Map<String, Object>> getTrees(List<SysMenu> menuList, boolean isCheck, List<String> roleMenuList,
                                              boolean permsFlag) {
        List<Map<String, Object>> trees = new ArrayList<>();
        for (SysMenu menu : menuList) {
            Map<String, Object> deptMap = new HashMap<>();
            deptMap.put("id", menu.getId());
            deptMap.put("pId", menu.getParentId());
            deptMap.put("name", menu.getName());
            if (isCheck) {
                deptMap.put("checked", roleMenuList.contains(menu.getId()));
            } else {
                deptMap.put("checked", false);
            }
            trees.add(deptMap);
        }
        return trees;
    }

    /**
     * 查询递归
     */
    private List<SysMenu> getMenuTree(List<SysMenu> menuList, List<String> menuIdList, long parentId) {
        List<SysMenu> result = new ArrayList<>();
        List<SysMenu> temp = new ArrayList<>();
        for (SysMenu entity : menuList) {
            Long id = entity.getId();
            if (!menuIdList.contains(String.valueOf(id)) || StringUtils.equals(entity.getType(), BaseConstant.MenuType.BUTTON.getValue())) {
                continue;
            }
            if (entity.getParentId().equals(parentId)) {
                SysMenu deptScopeMode = new SysMenu();
                deptScopeMode.setId(entity.getId());
                deptScopeMode.setName(entity.getName());
                deptScopeMode.setUrl(entity.getUrl());
                deptScopeMode.setIcon(entity.getIcon());
                deptScopeMode.setParentId(entity.getParentId());
                temp = getMenuTree(menuList, menuIdList, entity.getId());
                if (temp.size() > 0) {
                    deptScopeMode.setChildren(temp);
                }
                result.add(deptScopeMode);
            }
        }
        return result;
    }
}
